﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OreSystem.Entities;
using NHibernate;
namespace OreSystem.DAL {
    public class LevelDAL {       
        
        public void Add(Level level) {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession())
            {
                session.SaveOrUpdate(level);
                session.Flush();
            }
        }
        public void Delete(Level l)
        {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession())
            {
                session.Delete(l);
                session.Flush();
            }
        }

        public void Update(Level level)
        {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession())
            {
                session.Update(level);
                session.Flush();
            }
        }
        public Level GetLevelById(int id) {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession())
            {
                return session.Get<Level>(id);
            }
        }
        public IList<Level> GetALL()
        {

            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession())
            {
                return session.CreateQuery("from Level").List<Level>();
            }
        }
        public IList<Level> GetLevelByLevel(int layer)
        {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession())
            {
                return session.CreateQuery("from Level l where l.Layer=:layer").SetInt32("layer", layer).List<Level>();
            }
        }



        public IList<Level> GetListByKey(string keys, int layer) {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession()) {
                return session.CreateQuery("from Level l where l.Layer=:layer and l.Name like:keys").SetInt32("layer", layer).SetString("keys","%"+keys+"%").List<Level>();
            }
        }

        public void deletes(IList<object> idlist)
        {
            using (ISession session= DaoFactory.CreateSessionFactory().OpenSession())
            {
                foreach (var item in idlist)
                {
                    var entity = session.Get<Level>(item);
                    if (entity == null)
                    {
                        return; 
                    }
                    session.Delete(entity);
                }
                session.Flush();
            }
        }
    }
}
